using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;

using MakeIT.Framework.Data;
using MakeIT.SGI.ERP.Entidades;

namespace MakeIT.SGI.ERP.Negocios
{
	public partial class SgiDAO : DAO
	{

		public static CargoVO getCargo(int? idCargo, string cargo)
		{
			CargoVO cargoVO = null;
			List<CargoVO> listCargoVOs = getCargos(idCargo, cargo, null);
			if (listCargoVOs != null && listCargoVOs.Count > 0)
			{
				cargoVO = listCargoVOs[0];
			}
			return cargoVO;
		}

		public static CargoVO getCargo(int? idCargo)
		{
			return getCargo(idCargo, null);
		}

		public static List<CargoVO> getCargos()
		{
			return getCargos(null, null, null);
		}

		public static List<CargoVO> getCargos(int? idCargo)
		{
			return getCargos(idCargo, null, null);
		}

		public static List<CargoVO> getCargos(int? idCargo, string cargo)
		{
			return getCargos(idCargo, cargo, null);
		}

		public static List<CargoVO> getCargos(int? idCargo, string cargo, int? idFilial)
		{
			string where = "WHERE ";
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT cgo.idCargo as cgo_idCargo, cgo.cargo as cgo_cargo, cgo.idFilial as cgo_idFilial, fil.filial as fil_filial, cgo.Cadastro as cgo_Cadastro, cgo.CadastradoPor as cgo_CadastradoPor, cgo.Edicao as cgo_Edicao, cgo.EditadoPor as cgo_EditadoPor  ");
			query.AppendLine("FROM cargo as cgo LEFT OUTER JOIN filial as fil on cgo.idFilial=fil.idFilial  ");
			if (idCargo != null)
			{
				query.AppendLine(where + "cgo.idCargo=@idCargo ");
				where = "AND ";
			}
			if (cargo != null)
			{
				query.AppendLine(where + "cgo.cargo LIKE @cargo ");
				where = "AND ";
			}
			if (idFilial != null)
			{
				query.AppendLine(where + "cgo.idFilial=@idFilial ");
				where = "AND ";
			}


			query.AppendLine("ORDER BY cgo.cargo ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			if (idCargo!= null) cmd.Parameters.Add(getParameter(cmd, "@idCargo", idCargo));
			if (cargo!= null) cmd.Parameters.Add(getParameter(cmd, "@cargo", "%"+cargo+"%"));
			if (idFilial!= null) cmd.Parameters.Add(getParameter(cmd, "@idFilial", idFilial));


			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();

				List<CargoVO> listCargo = new List<CargoVO>();

				while (reader.Read())
				{
					CargoVO cargoVO = CargoVO.CreateVO();
					cargoVO.idCargo = reader.GetInt32(reader.GetOrdinal("cgo_idCargo"));
					cargoVO.cargo = reader.GetString(reader.GetOrdinal("cgo_cargo"));
					cargoVO.idFilial = reader.GetInt32(reader.GetOrdinal("cgo_idFilial"));
					if (!reader.IsDBNull(reader.GetOrdinal("fil_filial")))
						cargoVO.idFilialVO.filial = reader.GetString(reader.GetOrdinal("fil_filial"));
					cargoVO.cadastro = reader.GetDateTime(reader.GetOrdinal("cgo_Cadastro"));
					cargoVO.cadastradoPor = reader.GetString(reader.GetOrdinal("cgo_CadastradoPor"));
					cargoVO.edicao = reader.GetDateTime(reader.GetOrdinal("cgo_Edicao"));
					cargoVO.editadoPor = reader.GetString(reader.GetOrdinal("cgo_EditadoPor"));


					listCargo.Add(cargoVO);
				}
				reader.Close();
				return listCargo;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public static void setCargo(CargoVO cargoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("UPDATE cargo ");
			query.AppendLine("SET cargo=@cargo, idFilial=@idFilial,  Cadastro=@Cadastro, CadastradoPor=@CadastradoPor, Edicao=@Edicao, EditadoPor=@EditadoPor ");
			query.AppendLine("WHERE idCargo=@idCargo ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idCargo", cargoVO.idCargo));
			cmd.Parameters.Add(getParameter(cmd, "@cargo", cargoVO.cargo));
			cmd.Parameters.Add(getParameter(cmd, "@idFilial", cargoVO.idFilial));
			cmd.Parameters.Add(getParameter(cmd, "@Cadastro", cargoVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@CadastradoPor", cargoVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@Edicao", cargoVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@EditadoPor", cargoVO.editadoPor));

			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}

		public static long newCargo(CargoVO cargoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("INSERT INTO cargo ");
			query.AppendLine("  ( cargo, idFilial, Cadastro, CadastradoPor, Edicao, EditadoPor ) ");
			query.AppendLine("VALUES ( @cargo, @idFilial, @Cadastro, @CadastradoPor, @Edicao, @EditadoPor ) ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@cargo", cargoVO.cargo));
			cmd.Parameters.Add(getParameter(cmd, "@idFilial", cargoVO.idFilial));
			cmd.Parameters.Add(getParameter(cmd, "@Cadastro", cargoVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@CadastradoPor", cargoVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@Edicao", cargoVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@EditadoPor", cargoVO.editadoPor));


			try
			{
				cmd.ExecuteNonQuery();

				long newID = 0;
				if (tipo.Equals(DBType.MSSql))
				{
					cmd.CommandText = "SELECT IDENT_CURRENT('cargo') AS 'Identity' ";
					DbDataReader reader = cmd.ExecuteReader();
					reader.Read();
					newID = (long)reader.GetDecimal(reader.GetOrdinal("Identity"));
					reader.Close();
				}
				else if (tipo.Equals(DBType.MySql))
				{
					newID = ((MySql.Data.MySqlClient.MySqlCommand)cmd).LastInsertedId;
				}
				else if (tipo.Equals(DBType.ODBC))
				{
					//TODO implementar
				}
				else if (tipo.Equals(DBType.ORACLE))
				{
					//TODO implementar
				}
				return newID;
			}
			catch (Exception ex) { throw ex; }

		}

		public static void delCargo(CargoVO cargoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("DELETE FROM cargo ");
			query.AppendLine("WHERE idCargo=@idCargo ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idCargo", cargoVO.idCargo));


			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}
		public static int getCountCargoxIdFilial(int? idFilial)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idFilial) as ocorrencias FROM cargo WHERE IdFilial=@IdFilial ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdFilial", idFilial));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
	}
}
